﻿/*
    Copyright (c) 2011, Jeremy Randall (http://www.auburnrandall.com)
      
    All rights reserved.
 
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
 
    2. Redistributions in binary form must reproduce the above copyright 
    notice, this list of conditions and the following disclaimer in 
    the documentation and/or other materials provided with the distribution.
 
    3. The names of the authors may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
    OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.IO;

namespace FormEngine
{
    /// <summary>
    /// Handles interacting with a document. The document is described by a set
    /// of associated information defined within a FormProperties instance. This 
    /// class will retrieve the fields defined within the document and will 
    /// generate a copy of the document based on new field data.
    /// </summary>
    [Serializable()]
    public abstract class FormParser
    {
        [NonSerialized()]
        private XmlDefinition _xmlDefinition = null;

        [NonSerialized()]
        private Stream _destinationStream = null;

        /// <summary>
        /// Fields defined within the form.
        /// </summary>
        public FieldDetailCollection Fields { get; private set; }

        /// <summary>
        /// Path to the generated document.
        /// </summary>
        public string GeneratedFilePath { get; private set; }

        /// <summary>
        /// File representing the raw data contained within the submitted form.
        /// </summary>
        public XmlDefinition DataDefinition
        {
            get
            {
                if (_xmlDefinition == null)
                {
                    _xmlDefinition = new XmlDefinition(this);
                    _xmlDefinition.Save();
                }

                return _xmlDefinition;
            }
        }
            
        protected FormProperties FormProperties { get; private set; }

        protected Stream DestinationStream
        {
            get { return _destinationStream; }
        }

        protected abstract void ReadFormFields();
        protected abstract void UpdateFormFields();
        protected abstract string FileExtension { get; }

        /// <summary>
        /// Generates a new document, filling in form data based on data stored in 
        /// the Fields collection.
        /// </summary>
        public void GenerateDocument()
        {
            if (Fields == null)
            {
                throw new InvalidOperationException("Field collection must be defined prior to calling UpdateFields");
            }

            GeneratedFilePath = FileManager.GenerateTempFile(FileExtension);

            using (_destinationStream = new FileStream(GeneratedFilePath, FileMode.Create))
            {
                UpdateFormFields();
            }
        }

        /// <summary>
        /// Reads the fields defined within the document referenced by a FormProperties 
        /// instance.
        /// </summary>
        /// <param name="properties">Instance of FormProperties defining a document.</param>
        public void ReadFields(FormProperties properties)
        {
            Fields = new FieldDetailCollection();
            FormProperties = properties;
            ReadFormFields();
        }
    }
}
